<template>
	<view class="conterSlisy">
		<view class="">
			<view class="" >
				<image src="../../static/weixn.png" class="congerArr" style="margin-bottom: 100rpx;" mode=""></image>
				<button open-type="getUserInfo" class="conterArrCons" @getuserinfo="getUserInfo">微信登录</button>
				<button  class="conterArrConsBorrom" @click="openFanshu">暂不登录</button>
			</view>
		</view>
	</view>
</template>

<script>
	// import {onShareTimeline,onShareTimeline} from "../../utils/share.js";
	// console.log(share)
	import _data from '../../common/_data';
	import _get from '../../common/_get';
	import {
		Request
	} from '@/utils/request.js';
	// staticPhoto
	export default{
		data(){
			return {
				referral_code:"",
				token:uni.getStorageSync('token'),
				avatarUrl:'',
				avatar:require('../../static/132.jpeg'),
				theme:'',
				nickname:'',
				//phone:'',
				user_info:{},
			}
		},
		computed:{
			staticPhoto () {
			  return _data.static_url();
			},
		},
		onLoad(e) {
			this.getbase();
			this.referral_code=uni.getStorageSync('referral_code');
		},
		onUnload(){
			 uni.$off('data_user_info');
		},
		onShow() {
			 uni.$off('data_user_info');
			let _this=this;
			uni.$on('data_user_info', function (data) {
				console.log(data,'data_user_info')
				if(data.username && data.username!='微信用户'){
					uni.redirectTo({
					  url: '/pages/index/index'
					})
				}else {
					uni.redirectTo({
					  url: '/pages/personalData/index'
					})
				}
			  _this.my_data = data;
			  _this.user_info=data;
			   uni.setStorageSync('user_info', JSON.stringify(data));
			});
		},
	
		methods:{
			nicknameCone(e){
				this.nickname=e.detail.value
			},
			// this.$httpSend({
			//   path: '/im/action/groupState',
			//   data: { list_id: _this.list_id },
			//   success (res) {
			updateUserInfo(e){
				let _this=this;
				if(!this.avatarUrl){
					uni.showToast({
					  title: '请选择头像',
					  icon: 'none'
					});
					return false;
				} 
				if(!this.nickname){
					uni.showToast({
					  title: '请输入用户名称',
					  icon: 'none'
					});
					return false;
				}
				this.$httpSend({
					path:"/im/user/updateUserInfo",
					data:{
						username:_this.nickname,
						headimgurl:_this.avatarUrl,
					},
					success(e){
						uni.redirectTo({
						  url: '/pages/index/index'
						})
					}
				})
			},
			getImage (avatarUrl) {
				let _this=this;
				 this.$httpSendFile({
				   type: 1,
				   local_url: avatarUrl,
				   success (data) {
					_this.avatarUrl=data.img_url;
				   }
				 });
			},
			getbase(){
				if(this.token){
					_get.base();
				}
			},
			// 
			
			onChooseAvatar(e){
				console.log();
				let _this=this;
				  _this.getImage(e.detail.avatarUrl)
				// uni.getImageInfo({
				//   src: e.detail.avatarUrl,
				//   success: (image) => {
				// 	  console.log(image)
					
				//   }
				// });
				// https://im.numtwin.com/im/upload/photo
			},
			// /im/user/bindGroup
			openFanshu(){
				uni.navigateBack({ delta: 1 })
			},
			// /im/get/chatList
			 getUserInfo(e) {
				 console.log(e);
			      if (e.mp.detail.userInfo) {
			        this.userInfo = e.mp.detail.userInfo;
			        this.hasUserInfo = true;
			        this.login({
						userInfo:e.mp.detail.userInfo
					});
			      } else {
			        uni.showModal({
			          title: '警告',
			          content: '您点击了拒绝授权，将无法进入小程序，请授权之后再进入!',
			          showCancel: false,
			          confirmText: '返回授权',
			          success: function(res) {
			            if (res.confirm) {
			              console.log('用户点击了“返回授权”');
			            }
			          }
			        });
			      }
			    },
				 login(ObsJUs) {
					 let _thos=this;
				      uni.login({
				        provider: 'weixin',
				        success: (loginRes) => {
				          // 登录成功，获取用户code
						  console.log(ObsJUs)
				          const { code } = loginRes;
						  console.log(code)
						  // return false;
				          // 发送code到后台换取openId, sessionKey, unionId
				          uni.request({
				            url: 'https://im.xwjgene.com/im/in/getAuthLogin', // 你的登录API地址
				            method: 'POST',
				            data: {
				              code,
							  client_id:'',
							  referral_code:_thos.referral_code,
							  userinfo:JSON.stringify(ObsJUs.userInfo),
							  
				            },
				            success: (res) => {
							  console.log(res)
				              if (res.data.err==0) {
				                // 将用户信息和session存储到本地
								uni.setStorageSync('token', res.data.data.token);
								this.token=res.data.data.token;
								
								
								
								this.getbase();
								// uni.navigateBack({ delta: 1 })
								
				              } else {
				                uni.showToast({
				                  title: '登录失败',
				                  icon: 'none'
				                });
				              }
				            },
				            fail: () => {
				              uni.showToast({
				                title: '请求失败',
				                icon: 'none'
				              });
				            }
				          });
				        },
				        fail: (err) => {
				          console.log('uni.login 接口调用失败，将无法正常使用开放接口等服务', err);
				          uni.showToast({
				            title: '登录失败',
				            icon: 'none'
				          });
				        }
				      });
				    },
			
		}
	}
</script>

<style scoped>
	.conterSlisy{
		display: flex;
		align-items: center;
		min-height: 100vh;
		justify-content: center;
	}
	.congerArr{
		width: 128rpx;
		height: 128rpx;
		margin: 0 auto;
		display: flex;
	}
	.conterArrCons{
		width: 620rpx;
		height: 88rpx;
		text-align: center;
		background: #256FF3;
		border-radius: 50rpx 50rpx 50rpx 50rpx;
		font-family: PingFang SC, PingFang SC;
		font-weight: 500;
		font-size: 28rpx;
		margin-bottom: 20rpx;
		color: #FFFFFF;
		line-height: 88rpx;
		text-align: center;
		font-style: normal;
		text-transform: none;
	}
	.conterArrConsBorrom{
		width: 620rpx;
		height: 88rpx;
		box-sizing: border-box;
		border:1rpx solid #256FF3;
		text-align: center;
		background: #FFFFFF;
		border-radius: 50rpx 50rpx 50rpx 50rpx;
		font-family: PingFang SC, PingFang SC;
		font-weight: 500;
		font-size: 28rpx;
		color: #256FF3;
		line-height: 88rpx;
		text-align: center;
		font-style: normal;
		text-transform: none;
	}
	.avatar-wrapper{
		display: flex;
		padding: 0;
		border: none;
		margin: 0;
		width: 128rpx;
		height: 128rpx;
		margin: 0 auto;
		display: flex;
		margin-bottom: 28rpx;
	}
	.weui-input{
		width: 502rpx;
		height: 88rpx;
		margin: 0 auto;
		padding-left: 24rpx;
		border-radius: 16rpx 16rpx 16rpx 16rpx;
		border: 1rpx solid #C4CAD4;
		margin-bottom: 28rpx;
	}
	
</style>